<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.img.php');
$msgPag = reqVar('msgpag');
$sql_sel = "SELECT d.cod_tabella, d.descr, d.lista_campi, v.* " .
		" FROM ovf_tabelle_val v " .
		" INNER JOIN ovf_tabelle d ON d.id_tabella = v.id_tabella " .
		" WHERE v.id_tabella = ".intval(reqVar('id')).
		" AND LOWER(chiave) = '".reqVarAS('key')."'";
$objDB->query($sql_sel,'selRecDom');
$acRecDom = $objDB->fetch('selRecDom');
if (!$acRecDom) {
	die("tabelleView - manca id/chiave");
}
if (reqVar('azione') == 'salva') {
	$msgPag = confMod();
} 
if (reqVar('azione') == '') {
	$_REQUEST['chiave'] = $acRecDom['chiave'];
	$_REQUEST['scaduto'] = $acRecDom['scaduto'];
	for($kX=1; $kX<=12; $kX++) {
		$ncX = "c".str_pad($kX,2,'0',STR_PAD_LEFT);
		$_REQUEST[$ncX] = $acRecDom[$ncX];
	}		
} 
$aTipi = array("T"=>"Tabella","R"=>"Regola");
$aLabCampi = explode(",",$acRecDom['lista_campi']);

testaHtml('Gestione tabelle', 'Modifica elemento tabella',$msgPag, 'gest');
echo getHtmlForm();
echo getHtmlHidden('azione','salva');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('id');
echo getHtmlHidden('key');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
         &nbsp;&nbsp;&nbsp;
         <a class="cmd1" href="javascript:document.frm1.submit();" >salva</a>
			<? echo cmdTorna('torna'); ?>
      </td>
   </tr>
</table>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="lab1" width='20%' align='left'>Tabella</td>
      <td class="txt1" width='80%' align='left' colspan='3'>
			<span style='font-family:lucida console,courier;'><?=$acRecDom['cod_tabella']?></span>
			<?=' - '.$acRecDom['descr']?>
		</td>
   </tr>
<?
foreach($aLabCampi as $kX => $labX) {
	if ($kX == 0) {
?>
   <tr>
      <td class="lab1"><?=$labX?> (chiave)</td>
		<td class="txt1" width='60%'>
			<?=getHtmlText('chiave',40);?>
		</td>
		<td class="txt1" width='20%' colspan='2'>
			<?=getHtmlCheckBox('scaduto','S');?>&nbsp;scaduto
		</td>
   </tr>
<?
		continue;
	} 
	if ($kX > 12) {
		break;
	}
	$ncX = "c".str_pad($kX,2,'0',STR_PAD_LEFT);
?>
   <tr>
      <td class="lab1" ><?=$labX?></td>
		<td class="txt1" colspan='3'>
			<?=getHtmlText($ncX,80);?>
		</td>
   </tr>
<?
}
?>
</table>
<?
echo "<form/>\n";
codaHtml();
exit();
function confMod() {
	global $objDB;
	$msg = '';
	$chiave = trim(reqVar('chiave'));
	if (empty($chiave)) {
		$msg .= "campo chiave obbligatorio<br>";
	}
	if (strtolower($chiave) != strtolower(reqVar('key'))) {
		$sqlSel = "SELECT id_tabella,chiave FROM ovf_tabelle_val " .
				" WHERE id_tabella = " .intval(reqvar('id')).
				" AND LOWER(chiave) = '".addslashes(strtolower($chiave))."'" .
				" AND LOWER(chiave) <> '".addslashes(strtolower(reqVar('key')))."'";
		$objDB->exec($sqlSel);
		$aRow = $objDB->fetch();
		if ($aRow) {
			$msg .= "chiave gia' esistente<br>";
		}
	}
	if ($msg != '') {
		return $msg;
	}
	if (reqVar('scaduto')!='S') $_REQUEST['scaduto'] = 'N';
	$sSqlIns = "UPDATE ovf_tabelle_val SET chiave = '".addslashes($chiave)."'";
	$sSqlIns .= ", scaduto = '".reqvar('scaduto')."'";
	for($kX=1; $kX<=12; $kX++) {
		$ncX = "c".str_pad($kX,2,'0',STR_PAD_LEFT);
		if (isset($_REQUEST[$ncX])) {
			$sSqlIns .= ", $ncX = '".reqVarAS($ncX)."'";
		}
	}		
	$sSqlIns .= " WHERE id_tabella = ".intval(reqvar('id'));
	$sSqlIns .= " AND LOWER(chiave) = '".addslashes(strtolower(reqVar('key')))."'";
	$objDB->exec($sSqlIns);
	//return "<pre>".print_r($objDB,1)."</pre>";
	header("Location: tabelleValView.php?id=".intval(reqvar('id'))."&key=".urlencode($chiave).
		"&msgpag=".urlencode('chiave '.$chiave.' modificata')."&xsn=".XSN);
	exit();
}
?>